parser grammar AnalisisParser;

options {

  language = Java;
  tokenVocab = AnalisisLexer;
  backtrack = true;
  ASTLabelType = MiArbol;
  output = AST;
}


tokens
{
  LISTA_INST;
  INSTRS;
  FUNC;
  CALL;
  UNARIO;
}

@header{
  package practicaspl.doceuno;
  import java.util.Hashtable;
  
}


@members{
  Hashtable tablavar = new Hashtable();
  
  private ArrayList<RecognitionException> errors = new ArrayList<RecognitionException>();
  public ArrayList<RecognitionException> getErrors() {
    return errors;
  }
  @Override
  public void reportError (RecognitionException e) {
     String msg = getErrorMessage(e, getTokenNames());
     System.err.println(msg);
     errors.add(e);


  }
  
  public List<CommonTree> functionDefinitions = new ArrayList<CommonTree>();
}


instrucciones : (tipoentrada SEMICOLON)* EOF -> ^(INSTRS tipoentrada*);

tipoentrada : asignacion | expresion | funcion;

asignacion : IDENT IGUAL^ expresion;

funcion: IDENT O_PAR parametro C_PAR IGUAL expresion -> ^(FUNC IDENT parametro expresion) ;
  finally {
    functionDefinitions.add($funcion.tree);
  }

numero : NUMERO | REAL;
  
parametro : ID | NUMERO | REAL;

expresion : exp_mult ((MAS^|MENOS^) exp_mult)*;

exp_mult : exp_base ((MULT^|DIV^) exp_base)*;

exp_base : MENOS^ numero 
  | parametro 
  | O_PAR! expresion C_PAR! 
  | O_CORCHETE expresion C_CORCHETE -> ^(UNARIO expresion) 
  | IDENT O_PAR parametro C_PAR -> ^(CALL IDENT parametro)
  ;

